<template>
  <div class="trade-detail-page">
    <!-- 导航栏 -->
    <NavBar title="商品详情" />
    
    <div v-if="loading" class="loading-container">
      <van-loading size="24px">加载中...</van-loading>
    </div>
    
    <div v-else-if="tradeDetail" class="page-content">
      <!-- 商品图片 -->
      <div class="image-section">
        <van-swipe :autoplay="3000" indicator-color="white">
          <van-swipe-item v-for="(image, index) in images" :key="index">
            <img :src="image" alt="商品图片" @error="handleImageError" />
          </van-swipe-item>
        </van-swipe>
      </div>
      
      <!-- 商品信息 -->
      <div class="info-section">
        <div class="price-section">
          <span class="price">¥{{ tradeDetail.price }}</span>
        </div>
        
        <h1 class="title">{{ tradeDetail.title }}</h1>
        
        <div class="meta-info">
          <span class="author">{{ tradeDetail.nickName || '匿名用户' }}</span>
          <span class="time">{{ formatTime(tradeDetail.createTimestamp) }}</span>
        </div>
      </div>
      
      <!-- 商品描述 -->
      <div class="description-section">
        <h3>商品描述</h3>
        <p class="description">{{ tradeDetail.description }}</p>
      </div>
      
      <!-- 联系卖家按钮 -->
      <div class="contact-section">
        <van-button type="primary" block round @click="contactSeller">
          联系卖家
        </van-button>
      </div>
    </div>
    
    <div v-else class="empty-container">
      <van-empty description="商品不存在" />
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, onMounted, computed } from 'vue'
import { useRoute } from 'vue-router'
import { showToast } from 'vant'
import NavBar from '@/components/Common/NavBar.vue'
import { tradeApi, type TradeItem } from '@/api/trade'
import { formatTime } from '@/utils/format'

const route = useRoute()
const tradeId = route.params.id as string

const tradeDetail = ref<TradeItem | null>(null)
const loading = ref(true)

// 计算商品图片数组
const images = computed(() => {
  if (!tradeDetail.value) return ['/images/default-trade.png']
  
  const pics = [
    tradeDetail.value.pic1,
    tradeDetail.value.pic2,
    tradeDetail.value.pic3
  ].filter(Boolean)
  
  return pics.length > 0 ? pics : ['/images/default-trade.png']
})

// 加载商品详情
const loadDetail = async () => {
  try {
    loading.value = true
    const result = await tradeApi.getDetail(tradeId)
    
    if (result.code === 1) {
      tradeDetail.value = result.data
    } else {
      showToast(result.msg || '加载失败')
    }
  } catch (error) {
    console.error('加载商品详情失败:', error)
    showToast('加载失败')
  } finally {
    loading.value = false
  }
}

// 图片加载失败处理
const handleImageError = (e: Event) => {
  const target = e.target as HTMLImageElement
  target.src = '/images/default-trade.png'
}

// 联系卖家
const contactSeller = () => {
  showToast('请通过其他方式联系卖家')
}

onMounted(() => {
  loadDetail()
})
</script>

<style scoped>
.trade-detail-page {
  min-height: 100vh;
  background-color: #f1f1f1;
}

.loading-container,
.empty-container {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 50vh;
}

.page-content {
  padding-bottom: 80px;
}

.image-section {
  background: white;
  margin-bottom: 8px;
}

.image-section img {
  width: 100%;
  height: 300px;
  object-fit: cover;
}

.info-section {
  background: white;
  padding: 16px;
  margin-bottom: 8px;
}

.price-section {
  margin-bottom: 12px;
}

.price {
  font-size: 24px;
  font-weight: 600;
  color: #ff4444;
}

.title {
  font-size: 18px;
  font-weight: 600;
  color: #333;
  margin: 0 0 12px 0;
  line-height: 1.4;
}

.meta-info {
  display: flex;
  justify-content: space-between;
  align-items: center;
  font-size: 14px;
  color: #666;
}

.description-section {
  background: white;
  padding: 16px;
  margin-bottom: 8px;
}

.description-section h3 {
  font-size: 16px;
  font-weight: 600;
  color: #333;
  margin: 0 0 12px 0;
}

.description {
  font-size: 14px;
  color: #666;
  line-height: 1.6;
  white-space: pre-wrap;
}

.contact-section {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  padding: 12px 16px;
  background: white;
  border-top: 1px solid #eee;
}
</style>